*This file creates placebo estimates, using the specification defined in Equation 1



********************************
*****ANY CRIME****
********************************
use data_IL, clear
set more off

g censustract=substr(geo_id,10,.)


*creating date info
g day = day(date_m)
g dow = dow(date_m)
	* 0 = Sunday
g month = month(date_m)
g year = year(date_m)

g run_var = date_m - 18294
sort year month
g year_month = (year*100) + month
egen month_id = group(year_month)
bys month_id: egen m2 = min(run_var)
drop month_id run_var
rename m2 month_id

egen ct_id = group(geo_id)

tempname sim
postfile `sim' b2 se2 pv2 niter using 2007_2013_any, replace

** simulation setup **
global nobs 1000 // number of simulation
set seed 10101 
forvalues i=1/$nobs {


* randomly pick treatment dates
sort date_m geo_id
drop if date_m<18339 & date_m>18249  //based on 45 days cutoff on either side
by date_m: gen rannum=runiform()


bys date_m rannum: gen firsty=. //first date treatment, with randomization based on rannum
bys date_m rannum: gen firsty2=. //first date under treatment, using snap2 definition

egen rid=group(rannum)
sort rannum
gen pick=_n==1 //pick 1 treatment date based on randomization before 2010 (treatment year)
replace pick=0 if (pick==1 & year>2010)
replace pick=1 in 2 if (pick[_n-1]==0 & year<2011)
replace pick=1 in 3 if (pick[_n-1]==0 & pick[_n-2]==0 & year<2011)
replace pick=1 in 4 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & year<2011)
replace pick=1 in 5 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & year<2011)
replace pick=1 in 6 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & pick[_n-5]==0 & year<2011)
replace pick=1 in 7 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & pick[_n-5]==0 & pick[_n-6]==0 & year<2011)

bys date_m: egen treatment=total(pick)  //generate treatment variable
g treat_date=0
replace treat_date=date_m if pick>0
egen treat_date2=max(treat_date)
drop treat_date
rename treat_date2 treat_date

g run_var = date_m - treat_date
g post=0
replace post=1 if (date_m>=treat_date)
g pre = (date_m<treat_date & date_m)
g run_var_post = post*run_var
drop if run_var>45 | run_var<-45

// define treatment
replace firsty=1 if firsty2~=.
sort year month day
bys date_m: gen snap=sum(firsty) //running sum

gen snap2=snap
replace snap2=firsty2 if firsty2~=.

disp "now iteration has run `i' times" // iteration count


* choose specification
xi: areg any post run_var run_var_post `weather' i.year i.dow i.day, vce(cluster ct_id) absorb(ct_id)

scalar b2=_b[post]
scalar se2=_se[post]
scalar t2=b2/se2
scalar df2=e(N)-e(df_m)-1
scalar pv2=2*ttail(df2,abs(t2))
scalar niter=`i'

post `sim' (b2) (se2) (pv2) (niter)

drop rannum firsty firsty2 rid snap snap2 pick treatment treat_date run_var post pre run_var_post

}
postclose `sim'

* graph: all crimes
use 2007_2013_any, clear 
count if pv2<0.05
count if pv2<0.1
sort b2

graph twoway (kdensity b2), xline(-0.1439) title("Crime", margin(t=2 b=2)) xtitle("Estimated Coefficients", margin(t=2 b=2)) ytitle("K Density", margin(t=2 b=2)) graphregion(color(white) lc(white)) 
graph export "2007_2013_any.png", replace


********************************
*****THEFT****
********************************
use data_IL.dta, clear
set more off

g censustract=substr(geo_id,10,.)


*creating date info
g day = day(date_m)
g dow = dow(date_m)
	* 0 = Sunday
g month = month(date_m)
g year = year(date_m)

g run_var = date_m - 18294
sort year month
g year_month = (year*100) + month
egen month_id = group(year_month)
bys month_id: egen m2 = min(run_var)
drop month_id run_var
rename m2 month_id

egen ct_id = group(geo_id)

tempname sim
postfile `sim' b2 se2 pv2 niter using 2007_2013_theft, replace

** simulation setup **
global nobs 1000 // number of simulation
set seed 10101 
forvalues i=1/$nobs {


* randomly pick treatment dates
sort date_m geo_id
drop if date_m<18436 & date_m>18152  //based on 142 days cutoff on either side
by date_m: gen rannum=runiform()

bys date_m rannum: gen firsty=. //first date treatment, with randomization based on rannum
bys date_m rannum: gen firsty2=. //first date under treatment, using snap2 definition

*bys rannum: gen rid=_n //id based on rannum

egen rid=group(rannum)
sort rannum
gen pick=_n==1 //pick 1 treatment date based on randomization before 2010 (treatment year)
replace pick=0 if (pick==1 & year>2010)
replace pick=1 in 2 if (pick[_n-1]==0 & year<2011)
replace pick=1 in 3 if (pick[_n-1]==0 & pick[_n-2]==0 & year<2011)
replace pick=1 in 4 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & year<2011)
replace pick=1 in 5 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & year<2011)
replace pick=1 in 6 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & pick[_n-5]==0 & year<2011)
replace pick=1 in 7 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & pick[_n-5]==0 & pick[_n-6]==0 & year<2011)

bys date_m: egen treatment=total(pick)  //generate treatment variable
g treat_date=0
replace treat_date=date_m if pick>0
egen treat_date2=max(treat_date)
drop treat_date
rename treat_date2 treat_date

g run_var = date_m - treat_date
g post=0
replace post=1 if (date_m>=treat_date)
g pre = (date_m<treat_date & date_m)
g run_var_post = post*run_var
drop if run_var>142 | run_var<-142

// define snap & treatment
replace firsty=1 if firsty2~=.
sort year month day
bys date_m: gen snap=sum(firsty) //running sum

gen snap2=snap
replace snap2=firsty2 if firsty2~=.

disp "now iteration has run `i' times" // iteration count


* choose specification
xi: areg theft post run_var run_var_post `weather' i.year i.dow i.day, vce(cluster ct_id) absorb(ct_id)

scalar b2=_b[post]
scalar se2=_se[post]
scalar t2=b2/se2
scalar df2=e(N)-e(df_m)-1
scalar pv2=2*ttail(df2,abs(t2)) 
scalar niter=`i'

post `sim' (b2) (se2) (pv2) (niter)

drop rannum firsty firsty2 rid snap snap2 pick treatment treat_date run_var post pre run_var_post

}
postclose `sim'

* graph: theft
use 2007_2013_theft, clear 
count if pv2<0.05
count if pv2<0.1
sort b2

graph twoway (kdensity b2), xline(-0.0267) title("Theft", margin(t=2 b=2)) xtitle("Estimated Coefficients", margin(t=2 b=2)) ytitle("K Density", margin(t=2 b=2)) graphregion(color(white) lc(white)) 
graph export "2007_2013_theft.png", replace


					
******************
*Grocery store theft**
**********************

use ./Data/new_panel, clear
set more off

g censustract=substr(geo_id,10,.)


*creating date info
g day = day(date_m)
g dow = dow(date_m)
	* 0 = Sunday
g month = month(date_m)
g year = year(date_m)

g run_var = date_m - 18294
sort year month
g year_month = (year*100) + month
egen month_id = group(year_month)
bys month_id: egen m2 = min(run_var)
drop month_id run_var
rename m2 month_id

egen ct_id = group(geo_id)

scalar ti_any= "Crime"
scalar ti_theft_grocery = "Theft at Grocery Stores"
scalar ti_grocery = "Crime at Grocery Stores"
scalar ti_theft = "Theft"

**creating tables and graphs
loc REG_estout "substitute(_ \_ { $\left\{\text{ } }\right\}$) la collabels(none) eqlabels(none) posthead("") mlabels(none) prefoot("") postfoot("") varwidth(16) modelwidth(12) style(tex) starl(* .10 ** 0.05 *** 0.01)"


tempname sim
postfile `sim' b2 se2 pv2 niter using 2007_2013_theft_grocery, replace

** simulation setup **
global nobs 1000 // number of simulation
set seed 10101 
forvalues i=1/$nobs {


* randomly pick treatment dates
sort date_m geo_id
drop if date_m<18538 & date_m>18050 //based on the 244 days cutoff on either side
by date_m: gen rannum=runiform()

bys date_m rannum: gen firsty=. //first date treatment, with randomization based on rannum
bys date_m rannum: gen firsty2=. //first date under treatment, using snap2 definition

*bys rannum: gen rid=_n //id based on rannum

egen rid=group(rannum)
sort rannum
gen pick=_n==1 //pick 1 treatment date based on randomization before 2010 (treatment year)
replace pick=0 if (pick==1 & year>2010)
replace pick=1 in 2 if (pick[_n-1]==0 & year<2011)
replace pick=1 in 3 if (pick[_n-1]==0 & pick[_n-2]==0 & year<2011)
replace pick=1 in 4 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & year<2011)
replace pick=1 in 5 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & year<2011)
replace pick=1 in 6 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & pick[_n-5]==0 & year<2011)
replace pick=1 in 7 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & pick[_n-5]==0 & pick[_n-6]==0 & year<2011)

bys date_m: egen treatment=total(pick)  //generate treatment variable
g treat_date=0
replace treat_date=date_m if pick>0
egen treat_date2=max(treat_date)
drop treat_date
rename treat_date2 treat_date

g run_var = date_m - treat_date
g post=0
replace post=1 if (date_m>=treat_date)
g pre = (date_m<treat_date & date_m)
g run_var_post = post*run_var
drop if run_var>244 | run_var<-244

// define snap & treatment
replace firsty=1 if firsty2~=.
sort year month day
bys date_m: gen snap=sum(firsty) //running sum

gen snap2=snap
replace snap2=firsty2 if firsty2~=.


disp "now iteration has run `i' times" // iteration count


* choose specification
xi: areg theft_grocery post run_var run_var_post `weather' i.day i.year i.dow, vce(cluster ct_id) absorb(ct_id)

scalar b2=_b[post]
scalar se2=_se[post]
scalar t2=b2/se2
scalar df2=e(N)-e(df_m)-1
scalar pv2=2*ttail(df2,abs(t2)) //add 2 to avoid confusion- Cameron and Trivedi P151
scalar niter=`i'

post `sim' (b2) (se2) (pv2) (niter)

drop rannum firsty firsty2 rid snap snap2 pick treatment treat_date run_var post pre run_var_post

}
postclose `sim'

* graph: grocery theft
use 2007_2013_theft_grocery, clear 
count if pv2<0.05
count if pv2<0.1
sort b2

graph twoway (kdensity b2), xline(-0.0023) title("Grocery Thefts", margin(t=2 b=2)) xtitle("Estimated Coefficients", margin(t=2 b=2)) ytitle("K Density", margin(t=2 b=2)) graphregion(color(white) lc(white)) 
graph export "2007_2013_theft_grocery.png", replace

********************************
*****crime at grocery stores****
********************************
use ./Data/new_panel, clear
set more off

g censustract=substr(geo_id,10,.)


*creating date info
g day = day(date_m)
g dow = dow(date_m)
	* 0 = Sunday
g month = month(date_m)
g year = year(date_m)

g run_var = date_m - 18294
sort year month
g year_month = (year*100) + month
egen month_id = group(year_month)
bys month_id: egen m2 = min(run_var)
drop month_id run_var
rename m2 month_id

egen ct_id = group(geo_id)

tempname sim
postfile `sim' b2 se2 pv2 niter using 2007_2013_grocery, replace

** simulation setup **
global nobs 1000 // number of simulation
set seed 10101 
forvalues i=1/$nobs {


* randomly pick treatment dates
sort date_m geo_id
drop if date_m<18596 & date_m>17992  //based on 302 days cutoff on either side
by date_m: gen rannum=runiform()

bys date_m rannum: gen firsty=. //first date treatment, with randomization based on rannum
bys date_m rannum: gen firsty2=. //first date under treatment, using snap2 definition

egen rid=group(rannum)
sort rannum
gen pick=_n==1 //pick 1 treatment date based on randomization before 2010 (treatment year)
replace pick=0 if (pick==1 & year>2010)
replace pick=1 in 2 if (pick[_n-1]==0 & year<2011)
replace pick=1 in 3 if (pick[_n-1]==0 & pick[_n-2]==0 & year<2011)
replace pick=1 in 4 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & year<2011)
replace pick=1 in 5 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & year<2011)
replace pick=1 in 6 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & pick[_n-5]==0 & year<2011)
replace pick=1 in 7 if (pick[_n-1]==0 & pick[_n-2]==0 & pick[_n-3]==0 & pick[_n-4]==0 & pick[_n-5]==0 & pick[_n-6]==0 & year<2011)

bys date_m: egen treatment=total(pick)  //generate treatment variable
g treat_date=0
replace treat_date=date_m if pick>0
egen treat_date2=max(treat_date)
drop treat_date
rename treat_date2 treat_date

g run_var = date_m - treat_date
g post=0
replace post=1 if (date_m>=treat_date)
g pre = (date_m<treat_date & date_m)
g run_var_post = post*run_var
drop if run_var>99 | run_var<-99

// define snap & treatment
replace firsty=1 if firsty2~=.
sort year month day
bys date_m: gen snap=sum(firsty) //running sum

gen snap2=snap
replace snap2=firsty2 if firsty2~=.

disp "now iteration has run `i' times" // iteration count


* choose specification
xi: areg grocery post run_var run_var_post `weather' i.year i.dow, vce(cluster ct_id) absorb(ct_id)

scalar b2=_b[post]
scalar se2=_se[post]
scalar t2=b2/se2
scalar df2=e(N)-e(df_m)-1
scalar pv2=2*ttail(df2,abs(t2))
scalar niter=`i'

post `sim' (b2) (se2) (pv2) (niter)

drop rannum firsty firsty2 rid snap snap2 pick treatment treat_date run_var post pre run_var_post

}
postclose `sim'

* graph: grocery crimes
use 2007_2013_grocery, clear 
count if pv2<0.05
count if pv2<0.1
sort b2

graph twoway (kdensity b2), xline(-0.0028) title("Grocery Crimes", margin(t=2 b=2)) xtitle("Estimated Coefficients", margin(t=2 b=2)) ytitle("K Density", margin(t=2 b=2)) graphregion(color(white) lc(white)) 
graph export "2007_2013_grocery.png", replace



